package com.own.demo.长沙空中路网格子.v2;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.jts.生成osm;
import com.own.demo.utils.CommWriterFileInfo;
import com.own.demo.utils.DateUtils;
import com.own.demo.长沙空中路网格子.Thread.LonLat6Thread;
import com.own.demo.长沙空中路网格子.VMainParam;
import lombok.SneakyThrows;

import java.io.BufferedWriter;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

public class DrawEnter extends ParentV2 {

    private static String geoOSMUrl = "E:\\ownCode\\UMA\\数据\\路网数据\\base\\50米";
    private static String geoOSMName = "\\长沙市-50米.geojson.osm";

    private static String geoJson = "E:\\ownCode\\UMA\\数据\\路网数据\\base\\50米";
    private static String geoJsonName = "\\长沙市-50米.geojson";

    private static String geoJsonV1 = "E:\\ownCode\\UMA\\数据\\路网数据\\base\\50米";
    private static String geoJsonNameV1 = "\\polylineData1-50米.js";
    public static String urlAll = "E:\\ownCode\\UMA\\数据\\路网数据\\base\\50米";


    /**
     * @return
     */
    @SneakyThrows
    public static void goGeoJSONV2Local(VMainParam vMainParam) {
        if (vMainParam == null) {
            return ;
        }
        Date sTime = new Date();
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----全局解析数据：" + (DateUtils.formatTime(sTime, new Date())));
        JSONObject jsonObject = goGeoJSONV2(vMainParam.getGeometries(), vMainParam.getGeometry());
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----全局解析数据完成：" + (DateUtils.formatTime(sTime, new Date())));

        JSONArray jsonArray = jsonObject.getJSONArray("features");
        int row = jsonArray.size();
        CountDownLatch countDownLatch = new CountDownLatch(row);
        for (int i = 0; i < row; i++) {
            JSONArray coordinates = jsonArray.getJSONObject(i).getJSONObject("geometry").getJSONArray("coordinates");
            lonLat6All(countDownLatch, coordinates);
        }
        countDownLatch.await();
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----转换经纬度数据完成：" + (DateUtils.formatTime(sTime, new Date())));
        sTime = new Date();
        writerFile(jsonObject.getJSONArray("features"));
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----生成geojson完成：" + (DateUtils.formatTime(sTime, new Date())) +"地址："+geoJson+geoJsonName);

        sTime = new Date();
        生成osm.writeOsm(geoOSMUrl+geoOSMName,jsonObject,vMainParam.getBounds());
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----生成OSM完成：" + (DateUtils.formatTime(sTime, new Date())) +"地址："+geoOSMUrl+geoOSMName);

    }

    private static void lonLat6All(CountDownLatch countDownLatch, JSONArray coordinates)  {
        MainV2.executorService.execute(new LonLat6Thread(countDownLatch, coordinates));
    }

    private static  void writerFile(JSONArray jsonArray){
        int rowALlSize=jsonArray.size();
        BufferedWriter bufferedWriter = CommWriterFileInfo.getWriterBuffer(geoJson+geoJsonName, false);
        writerHeader(bufferedWriter);
        for(int i=0;i<rowALlSize-1;i++){
           JSONObject jsonObject1=jsonArray.getJSONObject(i);
           String json=JSONObject.toJSONString(jsonObject1)+",";
            CommWriterFileInfo.writer(bufferedWriter, json);
        }

       JSONObject jsonObject2= jsonArray.getJSONObject(rowALlSize-1);
        CommWriterFileInfo.writer(bufferedWriter,jsonObject2.toJSONString() );
        writerEnd(bufferedWriter);
    }
    private static void writerHeader(BufferedWriter bufferedWriter){
        String name="{\"features\": [";
        CommWriterFileInfo.writer(bufferedWriter, name);
    }

    private static void writerEnd(BufferedWriter bufferedWriter){
        String name="] , \"type\": \"GeometryCollection\"}";
        CommWriterFileInfo.writer(bufferedWriter, name);
    }

}
